package com.eascs.crm.nsEditDetail.controller;

import java.text.Format;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.common.util.UrlUtil;
import com.eascs.crm.nsEditDetail.entity.ContactsInfoEntity;
import com.eascs.crm.nsEditDetail.form.ContactsInfoForm;
import com.eascs.crm.nsEditDetail.service.ContactsInfoService;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.PaginationRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;

@Controller
@RequestMapping("/contactsInfo")
public class ContactsInfoRestController extends BaseRestController {
	private final Logger logger = LoggerFactory.getLogger(ContactsInfoRestController.class);
	@Autowired
	private ContactsInfoService contactsInfoService;
	@Autowired
	O2oLoanapplyService o2oLoanapplyService;

	// 修改
	@RequestMapping(value = "/update-index")
	public HtmlRestResult addIndex(String loanapplyNo, String contactsId) {
		if (StringUtils.isNotBlank(contactsId)) {
			ContactsInfoEntity entity = contactsInfoService.getById(contactsId);
			ViewContext.put("contactsInfo", entity);
		}else{
			ViewContext.put("loanapplyNo", loanapplyNo);
		}
		return HtmlRestResult.popHtmlFragment("/nsEditDetail/contacts_info_add");
	}

	@RequestMapping("/contacts-list")
	public PaginationRestResult house_list(String loanapplyNo,@RequestParam(defaultValue="0") Integer showPage, String approvalStatus, String type) {
		logger.info("紧急联系人列表查询单号：{}", loanapplyNo);
		O2oLoanapplyEntity o2oLoanapplyEntity = o2oLoanapplyService.findById(loanapplyNo);
		PageResult<Object[]> pageResult = contactsInfoService.findContactsListInfo(loanapplyNo,showPage,type,approvalStatus);
		String[] titles = {"姓名", "与借款人关系", "联系地址", "联系电话"};
		String url = "/contactsInfo/contacts-list";
		url = UrlUtil.appendUrl(url, "loanapplyNo", loanapplyNo) + "&showPage=";
		Button addressButton = null;
		Button phoneButton = null;
		Button editButton = null;
		Button delButton = null;
//		addressButton = ButtonHelper.createAjaxButton(null, "/contactsInfo/view_address?houseId=", "contactsLookAddress");
//		phoneButton = ButtonHelper.createAjaxButton(null, "/contactsInfo/view_address?houseId=", "contactsLookAddress");
//		if(!"detail".equals(type)){
//			addressButton = ButtonHelper.createAjaxButton(null, "/contactsInfo/view_address?houseId=", "UpdateInternalInfo");
//			phoneButton = ButtonHelper.createAjaxButton(null, "/contactsInfo/view_address?houseId=", "UpdateInternalInfo");
//			editButton = ButtonHelper.createAjaxButton("修改", "/contactsInfo/update-index?contactsId=", "UpdateInternalInfo");
//			delButton = ButtonHelper.createAjaxButton("删除 ", "/contactsInfo/contacts-del?contactsId=", "UpdateInternalInfo");
//		}
		if(StringUtil.isNotBlank(approvalStatus)){
			if (!"UNDO".equals(approvalStatus)) {
				addressButton = ButtonHelper.createAjaxButton(null, "/contactsInfo/view_address?houseId=", "UpdateInternalInfo");
				phoneButton = ButtonHelper.createAjaxButton(null, "/contactsInfo/view_address?houseId=", "UpdateInternalInfo");
			}
		}else {
			if (!"detail".equals(type)) {
				addressButton = ButtonHelper.createAjaxButton(null, "/contactsInfo/view_address?houseId=", "UpdateInternalInfo");
				phoneButton = ButtonHelper.createAjaxButton(null, "/contactsInfo/view_address?houseId=", "UpdateInternalInfo");
				if(StatusContexts.O2O_ZX_END.equals(o2oLoanapplyEntity.getWfType())){
					editButton = ButtonHelper.createAjaxButton("修改", "/contactsInfo/update-index?contactsId=", "UpdateInternalInfo");
					delButton = ButtonHelper.createAjaxButton("删除 ", "/contactsInfo/contacts-del?contactsId=", "UpdateInternalInfo");
				}
			}
		}
				
		Button[] titleButtons = new Button[] { null,null,addressButton,phoneButton };
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, url);
		Button[] detailButton = new Button[] { editButton, delButton };
		SelectDataOption[] selectDataOptions = { null,new SelectDataOption("YJKRGX"), null, null};
		paginationInfo.setSelectDataOptions(selectDataOptions);
		paginationInfo.setMoreButtons(detailButton);
		paginationInfo.setTitleButtons(titleButtons);
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setRendTo("#contacts-list");
		paginationInfo.setFormats(new Format[] { null, null, null  , null });
//		paginationInfo.setTitles(new String[] {null,"ss"});
		paginationInfo.setTitles(titles);
		
		ViewContext.put("paginationInfo", paginationInfo);
		return PaginationRestResult.buildPaginationRestResult(paginationInfo);
	}

	@RequestMapping(value = "/contacts-del")
	public BaseRestResult<String> houseDel(String contactsId) {
		int effectRow = contactsInfoService.delete(contactsId);
		if (effectRow <= 0) {
			return BaseRestResult.info("删除失败").andCloseActionDialog().andJquerySelectAjaxAction("#contacts-list");
		}
		return BaseRestResult.info("操作成功").andCloseActionDialog().andJquerySelectAjaxAction("#contacts-list");
	}

	@RequestMapping("/save")
	public BaseRestResult<String> contactsInfoSave(ContactsInfoForm form) {
		User loginUser = UserHolder.getUser();
		if (StringUtils.isBlank(form.getContactsId())) {
			ContactsInfoEntity contactsInfo = new ContactsInfoEntity();
			contactsInfo.setLoanapplyNo(form.getLoanapplyNo());
			contactsInfo.setContactsName(form.getContactsName());
			contactsInfo.setAddress(form.getAddress());
			contactsInfo.setType(form.getType());
			contactsInfo.setHomePhone(form.getHomePhone());
			contactsInfo.setIsDel("0");
			contactsInfo.setCreator(loginUser.getNo());
			contactsInfo.setCreateTime(new Date());
			ContactsInfoEntity saveResult = contactsInfoService.save(contactsInfo);
			if (saveResult == null) {
				return BaseRestResult.info("新增失败").andCloseActionDialog().andJquerySelectAjaxAction("#contacts-list");
			}
			return BaseRestResult.info("成功").andCloseActionDialog().andJquerySelectAjaxAction("#contacts-list");
		} else {
			ContactsInfoEntity contactsInfo = contactsInfoService.getById(form.getContactsId());
			contactsInfo.setContactsName(form.getContactsName());
			contactsInfo.setAddress(form.getAddress());
			contactsInfo.setType(form.getType());
			contactsInfo.setHomePhone(form.getHomePhone());
			contactsInfo.setUpdator(loginUser.getNo());
			contactsInfo.setUpdateTime(new Date());
			int effectRow = contactsInfoService.update(contactsInfo);
			if (effectRow > 0) {
				return BaseRestResult.info("成功").andCloseActionDialog().andJquerySelectAjaxAction("#contacts-list");
			} else {
				return BaseRestResult.info("修改失败").andCloseActionDialog().andJquerySelectAjaxAction("#contacts-list");
			}
		}
	}
	
	/**
	 *  CXW 贷款详情显示证件号码等信息星号 http://tick.o2ojr.eascs.com/ticket/view/loan-12592
	 * @param houseId
	 * @return
	 */
	@RequestMapping("/view_address")
	public BaseRestResult<String> view_address(String houseId) {
		ContactsInfoEntity contactsInfoEntity = contactsInfoService.getById(houseId);
		ViewContext.put("vo", contactsInfoEntity);
		return HtmlRestResult.popHtmlFragment("/nsEditDetail/contacts_view_info");
	}
}
